package model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Event {
	
	private boolean beingEdited=false;
	private static int IDcounter = 1;
	private int ID;
	private String title;
	private boolean isOpen = true;
	private GregorianCalendar date; // events should only be on Friday, Saturday and Sunday
	private int timeSlot; // should only be 0 or 1, because there is only 2 different times events can occur
	private int maxCapacity;
	private int currentCapacity;
	private Hall hall;
	private ArrayList<Booking> ListOfBookings = new ArrayList<Booking>();
	
	public void CreateBookings(String first, String last, int numberOfBookings){
		if(currentCapacity >= maxCapacity){
			System.out.println("Event is full");
			return;
		}
		if((currentCapacity+numberOfBookings) > maxCapacity){
			System.out.println("You cant book that many events. There are only " + (maxCapacity-currentCapacity) + " seats left");
			return;
		}
		if(beingEdited == true){
			System.out.println("file in use, please try again");
		}
		else{
			CreateBookingsCriticalSection(first, last, numberOfBookings);
		}
	}
	public synchronized void CreateBookingsCriticalSection(String first, String last, int numberOfBookings){
		beingEdited = true;
		for(int i=0; i<numberOfBookings; i++){
			Booking b = new Booking(first, last, this);
			ListOfBookings.add(b);
			currentCapacity++;
			b.save();
		}
		System.out.println(numberOfBookings + " bookings have been made successfully");
		this.save();
		beingEdited=false;
	}
	
	
	public Event(String title,GregorianCalendar date,int timeSlot, 
				int maxCapacity, Hall hall){
		this.ID = IDcounter;
		IDcounter++;
		this.title = title;
		this.date=date;
		this.timeSlot = timeSlot;
		this.maxCapacity = maxCapacity;
		this.hall = hall;
		save();
	}
	
	public int getID() {
		return ID;
	}

	//constructor to use when loading from file
	public Event(int ID, String title,GregorianCalendar date,int timeSlot, 
				int maxCapacity,int currentCapacity, Hall hall){
		this.ID = ID;
		this.title = title;
		this.date=date;
		this.timeSlot = timeSlot;
		this.maxCapacity = maxCapacity;
		this.currentCapacity = currentCapacity;
		this.hall = hall;
	}
	
	//iterate thru files in a directory
	public static void showFiles(File[] files) {
		try{
		    for (File file : files) {
		        if (file.isDirectory()) {
		            System.out.println("Directory: " + file.getName());
		            showFiles(file.listFiles()); // Calls same method again.
		        } else {
		            //System.out.println("File: " + file.getName());
		            int fileInt = Integer.parseInt(file.getName());
		            load(fileInt);
		        }
		    }
		}
		catch(NullPointerException e){
			e.printStackTrace();
			System.out.println("No event files were found\n");
		}
	}

	public static void loadAll(){
	    File[] files = new File("src\\data\\events\\").listFiles();
	    showFiles(files);
	}
	public static void load(int id){
		try {
			FileReader in = new FileReader("src\\data\\events\\"+id);
			Scanner src = new Scanner(in);
			int fileID = src.nextInt();
			String title = src.next();
			int year = src.nextInt();
			int month = src.nextInt();
			int day = src.nextInt();
			int slot = src.nextInt();
			int maxcap = src.nextInt();
			int currentcap = src.nextInt();
			String s_hall = src.next();
			
			Hall hall = null;
			if(s_hall.equals("Hall1")){
				hall = Hall1.getInstance();
			}
			if(s_hall.equals("Hall2")){
				hall = Hall2.getInstance();
			}
			if(s_hall.equals("Hall3")){
				hall = Hall3.getInstance();
			}
			GregorianCalendar date = new GregorianCalendar(year, month, day);
			Event e = new Event(id, title, date, slot, maxcap, currentcap, hall);
			//String text = in.readLine();
			//System.out.println(e);
			if(s_hall.equals("Hall1")){
				hall = Hall1.getInstance();
				Hall1.getInstance().add(e);
			}
			if(s_hall.equals("Hall2")){
				hall = Hall2.getInstance();
				Hall2.getInstance().add(e);
			}
			if(s_hall.equals("Hall3")){
				hall = Hall3.getInstance();
				Hall3.getInstance().add(e);
			}
			in.close();
		} catch (FileNotFoundException e) {
			System.out.println("File not found error");
			//e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Unexpected error");
			//e.printStackTrace();
		}
	}
	
	public String toFile(){
	return ID + " " + title + " " + date.get(Calendar.YEAR)+ " " + date.get(Calendar.MONTH) 
	 		+ " " + date.get(Calendar.DAY_OF_MONTH) + " " + timeSlot + " " 
	 		+ maxCapacity + " " + currentCapacity + " " + hall.getName();
	}
	
	public void save(){
		try {
			  // Create file 
			  FileWriter fstream = new FileWriter("src\\data\\events\\"+ID);
			  BufferedWriter out1 = new BufferedWriter(fstream);
			  out1.write(toFile());
			  //Close the output stream
			  out1.close();
		} 
		catch (IOException e) {
			System.out.println("Unexpected error");
			//e.printStackTrace();
		}
	}
	
	@Override
	public String toString() {
		return "ID=" + ID + ", title=" + title + ", isOpen=" + isOpen
				+ ", date=" + dateToString() + ", timeSlot=" + timeSlot 
				+ ", Capacity=" + currentCapacity + "/" + maxCapacity + ", Hall=" + hall.getName();
	}

	public String dateToString(){
		int year = date.get(Calendar.YEAR);
		int month = date.get(Calendar.MONTH);
		int day = date.get(Calendar.DAY_OF_MONTH);
		int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);	
		String dayName[] =
		{
		"Sunday",		
		"Monday",
		"Tuesday",
		"Wednesday",
		"Thursday",
		"Friday",
		"Saturday"
		};
		String monthName[] =
		{"January",
		"February",
		"march",
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"October",
		"Novemeber",
		"December"
		};
		String dayString = dayName[dayOfWeek-1];
		String monthString = monthName[month];
		
		return dayString + " " + monthString + " " + day + " " + year;
	}
	
}
